package cn.android.weblog.api;

import com.alibaba.fastjson.JSON;

import net.minidev.json.JSONObject;

import org.jsoup.Connection;

import java.io.IOException;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import cn.android.weblog.domain.Catagory;
import cn.android.weblog.domain.Passage;
import cn.android.weblog.util.HttpUtils;

public class CatagoryAPI {


    /**
     * 添加分类
     * @param catagory
     * @param userId
     * @return 添加成功/添加失败
     */
    public String addCatagory(Catagory catagory,Integer userId){
        Map<String,String> params=new HashMap<>();
        params.put("catagoryName",catagory.getCatagoryName());
        Connection.Response response = null;
        try {
            response= HttpUtils.post("/catagory/add/"+userId, JSONObject.toJSONString(params));
        } catch (IOException e) {
            e.printStackTrace();
        }
        System.out.println(response.body());
        return response.body();
    }

    /**
     * 添加收藏分类
     * @param catagory
     * @param userId
     * @return 添加成功/添加失败
     */
    public String addCollectCatagory(Catagory catagory,Integer userId){
        Map<String,String> params=new HashMap<>();
        params.put("catagoryName",catagory.getCatagoryName());
        Connection.Response response = null;
        try {
            response= HttpUtils.post("/catagory/addCollect/"+userId, JSONObject.toJSONString(params));
        } catch (IOException e) {
            e.printStackTrace();
        }
        Map<String,Object> results =(Map<String,Object>) JSON.parse(response.body());
        return results.get("Msg").toString();
    }

    /**
     * 根据用户ID查询分类列表
     * @param userId
     * @return
     */
    public List<Catagory> findCatagoryByUserId(Integer userId){
        Connection.Response response=null;
        try {
            response= HttpUtils.get("/catagory/findByUser/"+userId);
        } catch (IOException e) {
            e.printStackTrace();
        }
        Map<String,Object> results =(Map<String,Object>) JSON.parse(response.body());
        return JSON.parseArray(results.get("catagoryList").toString(),Catagory.class);
    }

    /**
     * 根据用户ID查询收藏分类列表
     * @param userId
     * @return
     */
    public List<Catagory> findCollectByUserId(Integer userId){
        Connection.Response response=null;
        try {
            response= HttpUtils.get("/catagory/findCollect/"+userId);
        } catch (IOException e) {
            e.printStackTrace();
        }
        Map<String,Object> results =(Map<String,Object>) JSON.parse(response.body());
        return JSON.parseArray(results.get("catagoryList").toString(),Catagory.class);
    }


    /**
     * 查询某一分类下的文章
     * @param catagory
     * @return
     */
    public List<Passage> findPassageByCatagoryId(Integer catagory) {
        Connection.Response response=null;
        try {
            response= HttpUtils.get("/catagory/findCatagory/"+catagory);
        } catch (IOException e) {
            e.printStackTrace();
        }
        Map<String,Object> results =(Map<String,Object>) JSON.parse(response.body());
        return JSON.parseArray(results.get("passageList").toString(),Passage.class);
    }


}
